<template>
  <div class="test-home-container">
    <el-menu
      class="sidebar-menu"
      :default-active="activeIndex"
      :collapse="false"
      @select="handleSelect"
    >
      <el-menu-item index="0">
        <el-icon><HomeFilled /></el-icon>
        <span>考卷信息</span>
        <span class="badge" v-if="store.getters.paperCount > 0">{{
          store.getters.paperCount
        }}</span>
      </el-menu-item>
      <el-menu-item index="1">
        <el-icon><List /></el-icon>
        <span>我的试卷</span>
      </el-menu-item>
      <el-menu-item index="2">
        <el-icon><DataAnalysis /></el-icon>
        <span>成绩分析</span>
      </el-menu-item>
      <el-menu-item v-if="route.path === '/test/startTest'" index="3">
        <el-icon><Timer /></el-icon>
        <span>开始考试</span>
      </el-menu-item>
      <el-menu-item v-if="route.path === '/test/answer'" index="4">
        <el-icon><Search /></el-icon>
        <span>查看试卷</span>
      </el-menu-item>
    </el-menu>
    <div class="main-content-test"><router-view /></div>
  </div>
</template>

<script setup>
import { ref, watchEffect, onMounted, onUnmounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";

const store = useStore();

const activeIndex = ref("0");
const route = useRoute();
const router = useRouter();

watchEffect(() => {
  if (route.path.includes("/test/answer")) {
    activeIndex.value = "4";
  } else if (route.path.includes("/test/startTest")) {
    activeIndex.value = "3";
  } else if (route.path.includes("/test/echarts")) {
    activeIndex.value = "2";
  } else if (route.path.includes("/test/result")) {
    activeIndex.value = "1";
  } else {
    activeIndex.value = "0";
  }
});

const handleSelect = (key) => {
  activeIndex.value = key;
  switch (key) {
    case "0":
      router.push("/test/index");
      break;
    case "1":
      router.push("/test/result");
      break;
    case "2":
      router.push("/test/echarts");
      break;
  }
};
</script>

<style scoped>
.test-home-container {
  background-color: #f5f5f5;
  display: flex;
  min-height: 80vh;
}

.sidebar-menu {
  width: 150px;
  position: sticky;
  top: 0;
  left: 0;
  border: solid 1px rgb(226, 226, 226);
}

.main-content-test {
  background-color: rgb(245, 245, 245);
  flex: 1;
  padding-left: 20px;
  padding-right: 20px;
  overflow-y: auto;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  background-color: red;
  color: white;
  border-radius: 50%;
  font-size: 12px;
  position: relative;
  top: -10px;
  right: -5px;
}
</style>
